/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "hhdTypes.h"
#include "hhdString.h"

#include <string.h>

namespace hhd
{
    class String;

    namespace HashFunctions
    {
        // raw hash functions

		inline uint32_t hash_DJB2(const char8_t* in_data, size_t in_size)
		{
			uint32_t result = 5381;

			for(uint32_t k = 0; k < in_size; k++)
			{
				result = ((result << 5) + result) + in_data[k];
			}
			return result;
		}

        // treats the incoming data as a char array and uses the djb2 hash algorithm
        // http://www.cse.yorku.ca/~oz/hash.html
        template <typename T>
		uint32_t primitive_hash(const T& in_t)
        {
            const char8_t* data = reinterpret_cast<const char8_t*>( &in_t );
			return hash_DJB2(data, sizeof(T));
		}

		template<>
		inline uint32_t primitive_hash<String>( String const& in_t)
		{
			return hash_DJB2( in_t, in_t.length() );
		}

		template<>
		inline uint32_t primitive_hash<String*>(String*const& in_t)
		{
			return hash_DJB2( (char8_t*const&)in_t, in_t->length() );
		}

		template<>
		inline uint32_t primitive_hash<>(char8_t* const& in_t)
		{
			size_t size = strlen(in_t);
			return hash_DJB2( in_t, size );
		}
    };
}
